package com.zwz.controller;

import com.zwz.dto.RestrictionDTO;
import com.zwz.entity.ConsumptionRestriction;
import com.zwz.service.ConsumptionRestrictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/restrictions")
public class ConsumptionRestrictionController {

    @Autowired
    private ConsumptionRestrictionService restrictionService;

    @PostMapping
    public ResponseEntity<?> create( String enterpriseId) {
        try {
            RestrictionDTO dto = restrictionService.createWithMain( enterpriseId );
            return ResponseEntity.status(HttpStatus.CREATED).body(dto);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (DataIntegrityViolationException e) {
            return ResponseEntity.badRequest().body("数据完整性错误");
        }
    }

    @GetMapping("/current")
    public ResponseEntity<List<ConsumptionRestriction>> getCurrent() {
        return ResponseEntity.ok(restrictionService.getCurrentRestrictions());
    }

    @GetMapping("/case/{caseNumber}")
    public ResponseEntity<List<ConsumptionRestriction>> getByCaseNumber(
            @PathVariable String caseNumber) {
        return ResponseEntity.ok(restrictionService.getByCaseNumber(caseNumber));
    }
}